export function downloadFn(data: BlobPart, name?: string): void {
     const link = document.createElement('a')
     const blob: any = new Blob([data], {
          type: 'application/vnd.ms-excel;charset=utf-8',
     })
     link.style.display = 'none'
     if (name) {
          link.download = name
     }
     link.href = window.URL.createObjectURL(blob)
     document.body.appendChild(link)
     link.click()
     document.body.removeChild(link)
     window.URL.revokeObjectURL(blob)
}

export function downArrloadFn(data: BlobPart, name?: string): void {
     const link = document.createElement('a')
     const blob: any = new Blob([data], {
          type: 'application/zip;charset=utf-8',
     })
     link.style.display = 'none'
     if (name) {
          link.download = name
     }
     link.href = window.URL.createObjectURL(blob)
     document.body.appendChild(link)
     link.click()
     document.body.removeChild(link)
     window.URL.revokeObjectURL(blob)
}

// 导出文档流方法
export const download = (res: any, type: any, filename: any) => {
     let Filetype = ''
     if (type == 'word') {
          Filetype = 'application/msword'
     } else if (type == 'zip') {
          Filetype = 'application/zip'
     } else if (type == 'pdf') {
          Filetype = 'application/pdf;chartset=UTF-8'
     } else if (type == 'excel') {
          Filetype = 'application/vnd.ms-excel'
     } else if (type == 'excels') {
          Filetype = '"application/octet-stream"'
     } else if (type == 'ppt') {
          Filetype =
               'application/vnd.openxmlformats-officedocument.presentationml.presentation'
     } else if (type == 'docx') {
          Filetype =
               'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
     }
     // 创建blob对象，解析流数据
     const blob = new Blob([res], {
          // 如果后端没返回下载文件类型，则需要手动设置：type: 'application/pdf;chartset=UTF-8' 表示下载文档为pdf，如果是word则设置为'application/msword'，zip为 'application/zip'
          type: Filetype,
     })
     const a = document.createElement('a')
     // 兼容webkix浏览器，处置webkit浏览器中href自动添加blob前缀，默认在浏览器打开而不是下载
     const URL = window.URL || window.webkitURL
     // 根据解析后的blob对象创建URL 对象
     const herf = URL.createObjectURL(blob)
     // 下载链接
     a.href = herf
     // 下载文件名,如果后端没有返回，可以自recordDialog己写a.download = '文件.pdf'
     a.download = filename
     document.body.appendChild(a)
     a.click()
     document.body.removeChild(a)
     // 在内存中移除URL 对象
     window.URL.revokeObjectURL(herf)
}
// 根据url
export function byUrlDownload(url: string, name?: string): void {
     const link = document.createElement('a')
     link.style.display = 'none'
     if (name) {
          link.download = name
     }
     link.href = url
     document.body.appendChild(link)
     link.click()
     document.body.removeChild(link)
}
